﻿using BrnShop.ConfigStrategy.File;
using BrnShop.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace XMLConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            City city1 = new City();
            city1.Name = "Shanghai";
            city1.Id = "SH001";
            city1.Areas = new Area()
            {
                Name = "Pudong",
                Id = "PD001",
                Streets = new string[] { "street 001", "street 002" }
            };

            XMLHelper.SaveToXml(IOHelper.GetMapPath("configs\\aa.xml"), city1);

            Main2();

            Console.ReadLine();
        }
        static void Main2()
        {
            City city1 = new City();
            var dd = XMLHelper.LoadFromXml(IOHelper.GetMapPath("configs\\aa.xml"), city1.GetType());

            Print(dd);
        }

        static void Main1(string[] args)
        {
            ConfigStrategy cs = new ConfigStrategy();
            var bb = cs.GetRDBSConfig();

            // var ss = aa.GetNCResponse();

            ufinterface obj = new ufinterface()
            {
                sendresult = new sendresult()
                {
                    billpk = "5162",
                    bdocid = "1221",
                    filename = "abbc",
                    resultcode = "31003",
                    resultdescription = "从输入流转换document出错：请检验文档格式。",
                    content = "2015-10-27"
                },roottag= "sendresult",
                successful = "N"
            };

            var r1 = cs.SaveNCResponse(obj);

            //var obj = cs.GetNCResponse();

            //Print(obj.sendresult);

            Console.ReadLine();
        }

        public static void Print(object model)
        {
            var t = model.GetType();

            foreach (PropertyInfo pi in t.GetProperties())
            {
                object value1 = pi.GetValue(model, null);//用pi.GetValue获得值

                if (value1 as IEntity != null)
                {
                    Print(value1);
                }

                string name = pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                                      //获得属性的类型,进行判断然后进行以后的操作,例如判断获得的属性是整数
                Console.WriteLine(string.Format("{1}={0}", value1, name));
            }
        }
    }

    public class IEntity { };

    [XmlRoot]
    public class City:IEntity
    {
        [XmlAttribute("别名")]
        public string Name
        {
            get;
            set;
        }

        [XmlAttribute]
        public string Id
        {
            get;
            set;
        }

        [XmlElement]
        public Area Areas
        {
            get;
            set;
        }
    }

    [XmlRoot]
    public class Area: IEntity
    {
        [XmlAttribute("地区名称")]
        public string Name
        {
            get;
            set;
        }
      
        public string Id
        {
            get;
            set;
        }

        public string[] Streets
        {
            get;
            set;
        }
    }
}
